package com.hyq.learn.api.rest;

import com.hyq.learn.web.env.LearnEnvironment;
import jakarta.annotation.Resource;
import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Set;

/**
 * 测试spring中应用上下文，也就是容器内容
 *
 * @author heyuqin
 * @date 2024/5/27 18:10
 */
@RestController
@RequestMapping("api/v1/public/app")
public class AppTestController implements ApplicationContextAware {

    private ApplicationContext context;

    @Resource
    private LearnEnvironment learnEnvironment;

    @GetMapping("env")
    public Set<String> getEnv() {
        return learnEnvironment.getActiveEnv();
    }

    @GetMapping("close")
    public void closeApp() {
        ConfigurableApplicationContext ctx = (ConfigurableApplicationContext) getContext();
        ctx.close();
    }

    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        this.context = applicationContext;
    }

    public ApplicationContext getContext() {
        return context;
    }

    public void setContext(ApplicationContext context) {
        this.context = context;
    }
}
